/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ufc.util;

import br.com.ufc.view.FormAgendaEti;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Paulo Silva
 */
public class HibernateUtil {

    private static Logger LOGGER = Logger.getLogger(HibernateUtil.class);
    private static SessionFactory factory;
    
    private static void buildFactory(){
        //Configuration configuration = new AnnotationConfiguration();
        //configuration.configure();
        //factory = configuration.buildSessionFactory();
        Configuration configuration = null;
        //if ("MYSQL".equals(appProp.getString("BANCO"))) {
        if ("MYSQL".equals(FormAgendaEti.getConfiguracoesSistema().getBancoDados())) {
            LOGGER.info("Abrindo configuração do MYSQL");
            configuration = new AnnotationConfiguration();
            configuration.configure("hibernateMySql.cfg.xml");
            factory = configuration.buildSessionFactory();
            //factory = new Configuration().configure("hibernateMySql.cfg.xml").buildSessionFactory();
        } else {
            LOGGER.info("Abrindo configuração do POSTGRESQL");
            configuration = new AnnotationConfiguration();
            configuration.configure("hibernatePostgreSQL.cfg.xml");
            factory = configuration.buildSessionFactory();
            //factory = new Configuration().configure("hibernatePostgreSQL.cfg.xml").buildSessionFactory();
        }
    }

    public static Session getSession() {
        //if (factory == null) {
            buildFactory();
        //}
        LOGGER.info("Abrindo uma nova sessão");
        return factory.openSession();
    }
}
